﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Tsanie.OsuDevice.Utils;
using System.IO;

namespace Tsanie.OsuDevice {
    public partial class OptionForm : Form {
        public OptionForm() {
            InitializeComponent();
        }

        private void Reload() {
            textOsuPath.Text = Config.OsuPath;
            textDownloadPath.Text = Config.GetDownloadPath(Config.DownloadPath, false);
            checkLogError.Checked = Config.LogError;
            numericTimeout.Value = Config.Timeout / 1000;
            checkTryOszSite.Checked = Config.TryOszSite;
        }

        private void OptionForm_Load(object sender, EventArgs e) {
            Reload();
        }
        protected override bool ProcessDialogKey(Keys keyData) {
            if (keyData == Keys.Escape) {
                this.Close();
                return false;
            }
            return base.ProcessDialogKey(keyData);
        }

        private void buttonOk_Click(object sender, EventArgs e) {
            if (!Directory.Exists(textOsuPath.Text)) {
                MessageBox.Show(this, "文件夹不存在，请重新选择！", "Osu程序路径", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textOsuPath.Focus();
                return;
            }
            if (!Directory.Exists(Config.GetDownloadPath(textDownloadPath.Text, true))) {
                MessageBox.Show(this, "文件夹不存在，请重新选择！", "Beatmap下载路径", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textDownloadPath.Focus();
                return;
            }
            if (textOsuPath.Text != Config.OsuPath)
                Config.SetValue("OsuPath", textOsuPath.Text);
            if (textDownloadPath.Text != Config.DownloadPath)
                Config.SetValue("DownloadPath", textDownloadPath.Text);
            if (checkLogError.Checked != Config.LogError)
                Config.SetValue("LogError", checkLogError.Checked);
            if (numericTimeout.Value * 1000 != Config.Timeout)
                Config.SetValue("Timeout", (numericTimeout.Value * 1000));
            if (checkTryOszSite.Checked != Config.TryOszSite)
                Config.SetValue("TryOszSite", checkTryOszSite.Checked);
            this.Close();
        }
        private void buttonReset_Click(object sender, EventArgs e) {
            Reload();
        }

        private void buttonOsuPath_Click(object sender, EventArgs e) {
            folderBrowser.Description = "请选择Osu程序所在目录。";
            folderBrowser.SelectedPath = textOsuPath.Text;
            if (folderBrowser.ShowDialog(this) == System.Windows.Forms.DialogResult.OK) {
                textOsuPath.Text = folderBrowser.SelectedPath;
            }
        }
        private void buttonDownloadPath_Click(object sender, EventArgs e) {
            folderBrowser.Description = "请选择Beatmap下载到的目录。";
            folderBrowser.SelectedPath = (textDownloadPath.Text.Length > 2 && textDownloadPath.Text[1] == ':' ? textDownloadPath.Text : Config.c_AppPath + textDownloadPath.Text);
            if (folderBrowser.ShowDialog(this) == System.Windows.Forms.DialogResult.OK) {
                textDownloadPath.Text = Config.GetDownloadPath(folderBrowser.SelectedPath, false);
            }
        }
    }
}
